﻿using DigitalHealth.PACS.ChatWebSocket.Context;
using DigitalHealth.PACS.ChatWebSocket.Services;
using DigitalHealth.PACS.Model;
using System.Collections.Generic;
using System.Linq;

namespace DigitalHealth.PACS.ChatWebSocket.Repositories.Imp
{

    internal class ChatGroupRepository : IChatGroupRepository
    {
        public ChatGroupRepository(ChatContext chatContext)
        {
            ChatContext = chatContext;
        }

        public ChatContext ChatContext { get; set; }
        public bool ChatGroupExists(string id)
        {
            return ChatContext.ChatGroups.Any(c => c.Id.ToString() == id);
        }

        public void Create(ChatGroup chatGroup)
        {
            ChatContext.ChatGroups.Add(chatGroup);
            ChatContext.SaveChanges();
        }

        public void Delete(ChatGroup chatGroup)
        {
            ChatContext.ChatGroups.Remove(chatGroup);
            ChatContext.SaveChanges();
        }

        public IEnumerable<ChatGroup> GetChatGroup()
        {
            return ChatContext.ChatGroups.ToList();
        }

        public IEnumerable<ChatGroup> GetChatGroupByChatUser(ChatUser user)
        {
            return ChatContext.ChatGroups.Where(c => c.users == user);
        }

        public ChatGroup GetChatGroupById(string id)
        {
            return ChatContext.ChatGroups.Find(id);
        }

        public void Update(ChatGroup chatGroup)
        {
            ChatContext.ChatGroups.Update(chatGroup);
            ChatContext.SaveChanges();
        }
    }
}
